<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>My first three.js app</title>
<style>
    body { margin: 0; }
</style>
</head>
<body>
<script src="./physics.js"></script>
<script type="module">

import * as THREE from 'https://unpkg.com/three@0.127.0/build/three.module.js';
const scene = new THREE.Scene();
const camera = new THREE.PerspectiveCamera( 75, window.innerWidth / window.innerHeight, 0.1, 1000 );
scene.background = new THREE.Color( 0xcce0ff );
scene.fog = new THREE.Fog( 0xcce0ff, 500*10, 10000 );
scene.add( new THREE.AmbientLight( 0x666666 ) );
camera.position.z = 5;

const renderer = new THREE.WebGLRenderer();
renderer.setSize( window.innerWidth, window.innerHeight );
document.body.appendChild( renderer.domElement );

var physics = new Physics()
physics.dt = 1/1024
var p1 = new Particle(physics, 0, 0, 1)
p1.needUpdateV = false
var p2 = new Particle(physics, 1, 0, 1)
p2.F = new Float32Array([0, 0, -10])
new Constraint(physics, p1, p2)

const geometry = new THREE.SphereGeometry( 0.3, 32, 32 );
const material = new THREE.MeshBasicMaterial( { color: 0xff0000 } );
const ball = new THREE.Mesh( geometry, material );
scene.add( ball );


p2.onSimulate = function(){
    const c = 2
    ball.position.x = this.pos[0]*c
    ball.position.y = this.pos[2]*c
    ball.position.z = this.pos[1]*c
}

var p3 = new Particle(physics, 2, 0, 1)
p3.F = new Float32Array([0, 0, -10])
new Constraint(physics, p2, p3)

const geometry2 = new THREE.SphereGeometry( 0.3, 32, 32 );
const material2 = new THREE.MeshBasicMaterial( { color: 0x00ff00 } );
const ball2 = new THREE.Mesh( geometry2, material2 );
scene.add( ball2 );


p3.onSimulate = function(){
    const c = 2
    ball2.position.x = this.pos[0]*c
    ball2.position.y = this.pos[2]*c
    ball2.position.z = this.pos[1]*c
}

const animate = function () {
    requestAnimationFrame( animate );
    for(let i=0; i<16; i++){
        physics.Simulate()
    }
    renderer.render( scene, camera );
};

animate();

</script>
</body>
</html>